package com.hnhd.eorder.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.hnhd.eorder.vo.DishType;

/**
 * 菜品与类型数据操作类
 * 
 * @author peng
 * 
 */
public class DishTypeUtil {
	
	
	
	/**
	 * 获取菜品类型表（T_DishType）中类型id对应的菜品id
	 * 
	 * @param typeId
	 * @return ids（对应菜品id的集合）
	 */
	public static ArrayList<Integer> getDishIdsByTypeId(int typeId) {
		Cursor cursor = SQLUtil.query(SQLUtil.DISHTYPE, null, "typeID=?",
				new String[] { String.valueOf(typeId) }, null, null, null);
		ArrayList<Integer> ids = new ArrayList<Integer>();
		if (cursor.moveToFirst()) {
			do {
				ids.add(cursor.getInt(0));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return ids;
	}
	

	public static ArrayList<DishType> getAllDishTypes() {
		Cursor cursor = SQLUtil.getReadableDatabase().rawQuery(
				"SELECT * FROM " + SQLUtil.DISHTYPE, null);
		ArrayList<DishType> dishTypes = new ArrayList<DishType>();
		if (cursor.moveToFirst()) {
			do {
				DishType dishType = new DishType();
				dishType.setDishId(cursor.getInt(0));
				dishType.setTypeId(cursor.getInt(1));
				dishTypes.add(dishType);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return dishTypes;
	}

	public static long insertDishType(DishType dishType) {
		ContentValues values = new ContentValues();
		values.put("dishId", dishType.getDishId());
		values.put("typeId", dishType.getTypeId());
		return SQLUtil.getWritableDatabase().insert(SQLUtil.DISHTYPE, null,
				values);
	}
	

	public static void deleteAllDishTypes() {
		SQLUtil.getWritableDatabase().delete(SQLUtil.DISHTYPE, null,null);
	}
}
